//
//  CourseViewController.swift
//  BellManage
//
//  Created by Bestsch on 16/3/31.
//  Copyright © 2016年 wsl. All rights reserved.
//

import UIKit

class CourseViewController: BaseViewController,UITableViewDelegate,UITableViewDataSource {
    
    var tableView: UITableView!
    
    var courseArray = NSMutableArray()
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Do any additional setup after loading the view.
        self.title = "课程表"
        
        InitViews()
        
    }
    
    func InitViews(){
        
        tableView = UITableView.init(frame: CGRect(x: 0, y: 0, width: screenW, height: screenH ))
        tableView.backgroundColor = UIColor.init(hexString: "f6f6f6", alpha: 1)
        tableView.delegate = self
        tableView.dataSource = self
        tableView.separatorStyle = UITableViewCellSeparatorStyle.singleLine
        self.view.addSubview(tableView)
        setUpRefresh()
        
    }
    func setUpRefresh() {
        tableView.header = MJRefreshNormalHeader.init(refreshingTarget: self, refreshingAction: #selector(HonorViewController.refresh))
        self.tableView.header.beginRefreshing()
    }
    /**
     刷新数据
     t=14    学校课程列表
     XmlNode schseridxml = ndoc.SelectSingleNode("//rss/channel/sch");
     
     
     */
    func refresh() {
        if (reachability?.isReachable())! {
            let user = getUserPojo()
            let url = server + adminwebapppi
            let request = requestHeader + "14</t><sch>" + user.schid + "</sch></channel></rss>"
            
            courseArray = getData(url, request: request)
            tableView.reloadData()
            
        } else {
            HYLog("网络连接：不可用")
        }
        
        tableView.header.endRefreshing()
    }
    
    /**
     高度
     
     - parameter tableView: tableview
     - parameter indexPath: item
     
     - returns: 高度
     */
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        
        return 60
        
    }
    /**
     多少行
     
     - parameter tableView: tableview
     - parameter section:
     
     - returns: 行数
     */
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return courseArray.count
        
    }
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    /**
     数据
     {"classid":"SA406","classname":"小四(6)班","schserid":"100100","userid":142551,"teaname":"王义","imageurl":"../../upload/classlesson/img/99c20ba6f90f4d57890ddfffe1febe8f.png                                                                                                                                      ","datatime":"2016-03-09T11:20:15"}
     
     - parameter tableView:
     - parameter indexPath:
     
     - returns:
     */
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cellIdentifier = "CourseTableViewCellIdentifier";
        
        let cell: CourseTableViewCell = CourseTableViewCell.init(style: UITableViewCellStyle.default, reuseIdentifier: cellIdentifier)
        cell.accessoryType = UITableViewCellAccessoryType.disclosureIndicator
        cell.selectionStyle = UITableViewCellSelectionStyle.none;
        let dic = courseArray[indexPath.row] as! JSON
        
        let imageUrl = dic["imageurl"].stringValue
        let classname = dic["classname"].stringValue
        
        cell.classname.text = classname
        if imageUrl.characters.count == 0 {
            cell.read.image = UIImage.init(named: "laba")
        }else {
            cell.read.image = UIImage.init(named: "laba2")
        }
        
        return cell
        
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let dic = courseArray[indexPath.row] as! JSON
        
        let imageUrl = dic["imageurl"].stringValue
        let classname = dic["classname"].stringValue
        if imageUrl.characters.count == 0 {
            TSMessage.showNotification(in: self, title: "提示", subtitle: "\(classname),班级尚未上传课程表，请联系班主任老师进行上传", image: nil, type: TSMessageNotificationType.error, duration: 2.0, callback: nil, buttonTitle: "知道了", buttonCallback: nil, at: TSMessageNotificationPosition.top, canBeDismissedByUser: true)
        }else {
            let contentVC = CourseContentViewController()
            contentVC.dic = dic
            self.navigationController?.pushViewController(contentVC, animated: true)
        }
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    
    /*
     // MARK: - Navigation
     
     // In a storyboard-based application, you will often want to do a little preparation before navigation
     override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
     // Get the new view controller using segue.destinationViewController.
     // Pass the selected object to the new view controller.
     }
     */
    
}
